/**
 * 商品列表页-ajax分页效果
 * by:wankang
 * date:2015年4月28日
 */
//页码
var page = 6;
//分类ID
var cid = '';
//搜索关键字
var key = '';
//最小价格
var minPrice = '';
//最大价格
var maxPrice = '';
//价格排序0：无排序，1：高到低，2：低到高
var priceOrder = 0;
//销量排序0：无排序，1：销量从高到低
var salesOrder = 0;
//加载产品的dom
var finder = '';
//平台ID
var userid = '';
//判断是否点击标签
var tag = 1;
//判断调用哪个方法标签
var actionType = '';
//url
var url = '';
//身份验证
var fxuserid = '';
//筛选标签
var sxbtnTag = '';
//总页数
var countPage = '';
//总条数
var totleNums = '';
/**
 * 数据请求方法
 * */
function loadProd(className, o_cid, o_key, o_userid, o_Type, o_url, o_fxuserid)
{
	if(tag == 1)
	{
		fxuserid = o_fxuserid;
		url = o_url;
		actionType = o_Type;
		finder = className;
		cid = o_cid;
		key = o_key;
		userid = o_userid;
		$.ajax({
			url:"/Mobilegoodsdp/ajax-load-prod",
			type:"post",
			dataType: 'json',
			data : {'key':key,'userid':userid,'cid':cid,'currpage':page,'minPrice':minPrice,'maxPrice':maxPrice,'priceOrder':priceOrder,'salesOrder':salesOrder},
			success: function(data){
				if(data.code=='100')//非首页所有商品加载完毕
				{	
					$(".loading").html("所有加载完毕!");
					tag = 0;
				}
				else if(data.code=='101')//价格筛选、分类搜索无商品
				{	
					if(minPrice =='' &&　maxPrice == '')
					{
						$('.loading').hide();
						$('.top_tips').css('margin-top','10px').show();
					}
					else
					{
						$('.loading').hide();
						$('.top_tips').css('margin-top','60px').show();
					}
					tag = 0;
				}
				else if(data.code=='102')//关键字未搜索到商品，显示推荐商品(店铺中按照销量排名36个产品)
				{
					$('.top_tips').css('margin-top','10px').html('抱歉,未找到符合条件的商品,向您推荐:').show();
					$('.loading').hide();
					var obj = eval(data);
					var pageSize = data.page_size;
					countPage = data.count_page;
					if(page == 6 &&　actionType =='')
					{
						$('.'+finder).html('');
					}
					else if(page == 6 &&　actionType !='')
					{
						$('.ul_list-3a').html('');
    					$('.ul_list-3b').html('');
					}
					if(actionType != '')
					{
						show_prod2(obj, pageSize);
					}
					else
					{
						show_prod(obj, finder, pageSize);
					}
					
					page = page+1;
				}
				else
				{	
					$('.top_tips').hide();
					$('.loading').html('点击加载更多...').show();
					var obj = eval(data);
					var pageSize = data.page_size;
					totleNums = data.count;
					countPage = data.count_page;
					if(page == 6 &&　actionType =='')
					{
						$('.'+finder).html('');
					}
					else if(page == 6 &&　actionType !='')
					{
						$('.ul_list-3a').html('');
    					$('.ul_list-3b').html('');
					}
					if(actionType != '')
					{
						show_prod2(obj, pageSize);
					}
					else
					{
						show_prod(obj, finder, pageSize);
					}
					
					page = page+1;
					
				}
				
			}
	  });
	}
	

}
/**
 * list_3页面专用
 * */
function show_prod2(obj, pageSize)
{
	if(typeof(obj.data)=="object")
	{
		var hita = 0, hitb = 1;
		var dataobj=obj.data;
		 var html = "";
		 var counter = 0;
		 $.each(dataobj, function (n, value)
		 {
			 var lable = '';
			 var simlabel = ''; 
			 if(value.labelStatus == 1)//判断标签是否存在
			 {
				 lable = "class = 'p_lable0"+value.style+"'";
				 simlabel = value.simlabel;
			 }
			 if(value.hid_link_url)//判断是否有外链
			{
				 var href = value.hid_link_url;
			}
			 else
			{
				 var href = ""+url+"/mobilegoodsdp/productdetails?id="+value.id+"&fxuserid="+fxuserid+"";
			}
			 if(value.name.length > 10)//判断是否需要截取商品名称
			{
				var prodName = strsub(value.name);
			}
			 else
			{
				var prodName = value.name;
			}
			 counter = n + 1;
             html = $("<li><a href='"+href+"'><img src='"+value.org+"' width='100%'><div class='pd_w'><p><span class='price_sale'>￥"+value.present_price+"</span></p><h2>"+prodName+"</h2><span class='buy2'>已售"+value.goodsnumbersold+"件</span></div></a><i "+lable+">"+simlabel+"</i></li>");
           if(hita < hitb)//瀑布流显示
           {
        	 html.appendTo(".ul_list-3a");
             hita = hita + html.height();
           }
           else
           {
        	 html.appendTo(".ul_list-3b");
             hitb = hitb + html.height();
           }
         });
		 if(page == '6')
	      {	
			 if(counter == totleNums)
		      {	
		    	  $(".loading").hide();
		    	  tag = 0;//防止点击 
			  }
	    	  
		  }
	      else if(counter < pageSize && page <= countPage)
	      {
	    	  $(".loading").html("所有加载完毕");
	    	  tag = 0;//防止点击 
	      }
	      else
	      {
			  $('.loading').html('点击加载更多...');
		  }
	}
	else
	{
		$(".loading").html("所有加载完毕!");
		tag = 0;
	}
}
/**
 * 商品名称字符串截取
 * */
function strsub(str)
{
	var new_str = str.substr(0, 10); 
	new_str = new_str+'...';
	return new_str;
}
/**
 * list_1-2-4页面专用
 * */
function show_prod(obj, finder, pageSize)
{	
	if(typeof(obj.data)=="object")
	{
		//判断点击事件是否被取消了
		var dataobj = obj.data;
		 var html = "";
		 var counter = 0;
		 $.each(dataobj, function (n, value)
		 {
			 var lable = ''
			 var simlabel = ''; 
			 if(value.labelStatus == 1)//判断标签是否存在
			 {
				 lable = "class = 'p_lable0"+value.style+"'";
				 simlabel = value.simlabel;
			 }
			 if(value.hid_link_url)//判断是否有外链
			{
				 var href = value.hid_link_url;
			}
			 else
			{	
				 var href = ""+url+"/mobilegoodsdp/productdetails?id="+value.id+"&fxuserid="+fxuserid+"";
			}
			 if(value.name.length > 10)//判断是否需要截取商品名称
			 {
				var prodName = strsub(value.name);
			 }
			 else
			 {
				var prodName = value.name;
			 }
			 counter = n + 1;
             html += "<li><a href='"+href+"'><img src='"+value.org+"' width='100%'><div class='pd_w'><h2>"+prodName+"</h2><p><span class='price_sale'>￥"+value.present_price+"</span><span class='buy'>已售"+value.goodsnumbersold+"件</span></p></div></a><i "+lable+">"+simlabel+"</i></li>";
     
         });
	      $minUl = getMinUl();
	      $minUl.append(html);
	      if(page == '6')
	      {		
		      if(counter == totleNums)
		      {	
		    	  $(".loading").hide();
		    	  tag = 0;//防止点击 
			  }
		  }
	      else if(counter < pageSize && page <= countPage)
	     {
	    	  $(".loading").html("所有加载完毕");
	    	  tag = 0;//防止点击 
	     }
	      else
	      {
			  $('.loading').html('点击加载更多...');
		  }
	}
	else
	{
		$(".loading").html("所有加载完毕!");
		tag = 0;//防止点击 
	}
	//图片宽高
   // var width = $('li').width(); 
  //  $('li').css('height', width+'px');
	
}
/**
 * 顺序加载
 * */
function getMinUl()
{
	//每次获取最短的ul,将图片放到其后
    var $arrUl = $('.'+finder);
    var $minUl =$arrUl.eq(0);
    $arrUl.each(function(index,elem)
    {
      if($(elem).height() < $minUl.height())
      {
        $minUl = $(elem);
      }
    });
    return $minUl;
 }
/**
 * 页面顶部按钮事件
 * */
$('.onClick').live('click',function()
{
	var obj = $(this);
	var attr = obj.attr('pvalue');
	var orderValue = obj.attr('order');
	if(attr == 'num')
	{
		//销量排序
		if(orderValue == 0)
		{
			salesOrder = '1';
			obj.attr('order','1');
			obj.css('background-color','#f3f3f3');
			page = 6;
			//取消价格排序
			var obj2 = $('.onClick:eq(1)').find('i');
			if(priceOrder == '1')
			{
				obj2.removeClass('pricedown');
			}
			else if(priceOrder == '2') 
			{
				obj2.removeClass('priceup');
			}
			priceOrder = 0;
		}
		else
		{
			salesOrder = '0';
			obj.attr('order','0');
			obj.css('background-color','');
			page = 6;
		}
		tag = 1;
		loadProd(finder, cid, key, userid, actionType);
	}
	else if(attr == 'price')
	{
		if(orderValue != '1')
		{
			//价格从高到低
			obj.attr('order','1');
			priceOrder = '1';
			page = 6;
			obj.find("i").removeClass('priceup');
			obj.find("i").addClass('pricedown');
		}
		else if(orderValue == '1')
		{
			//价格从低到高
			obj.attr('order','2');
			priceOrder = '2';
			page = 6;
			obj.find("i").addClass('priceup');
			obj.find("i").removeClass('pricedown');
		}
		salesOrder = 0;
		$('.onClick:first').attr('order','0');
		$('.onClick:first').css('background-color','');
		tag = 1;
		loadProd(finder, cid, key, userid, actionType);
	}
	else if(attr == 'filtrate')
	{
		//价格筛选
		$('#price').show();
	}
	
	
});
/**
 * 价格筛选事件
 * */
$('.sxbtn').bind('click',function()
{
	var min = $('.inp:first').val();
	var max = $('.inp:last').val();
	if(min != '' || max != '')
	{
		if(isNaN(min) !== true && min != '')
		{
			minPrice = min;
		}
		if(isNaN(max) !== true && max != '')
		{
			maxPrice = max;
		}
		if(minPrice != '' || maxPrice != '')
		{
			page = 6;
			tag = 1;
			sxbtnTag = 1;
			loadProd(finder, cid, key, userid, actionType);
		}
		else
		{
			alert('输入格式不正确');
		}
		
	}
	else
	{
		alert('请输入价格');
	}	
});
/**
 * 关闭价格筛选事件
 * */
$('.i_delete').bind('click',function()
{
	$('#price').hide();
	$('.inp:first').val('');
	$('.inp:last').val('');
	if(sxbtnTag == 1)
	{
		minPrice = '';
		maxPrice = '';
		tag = 1;
		page = 6;
		sxbtnTag = '';
		loadProd(finder, cid, key, userid, actionType);
	}
	
});
